package org.bootstrap.controller;

import cn.hutool.core.lang.Console;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.bootstrap.annotation.TokenValidate;
import org.bootstrap.entity.UserInfo;
import org.bootstrap.service.Impl.FileServiceImpl;
import org.bootstrap.service.Impl.UserServiceAdminImpl;
import org.bootstrap.service.Impl.UserServiceImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;

@RestController
@CrossOrigin
@RequestMapping(value = "user", method = {RequestMethod.POST, RequestMethod.GET, RequestMethod.OPTIONS})
public class UserController {
    @Resource
    private UserServiceImpl userService;

    @Resource
    private UserServiceAdminImpl userServiceAdmin;

    @Resource
    private FileServiceImpl fileService;

    /**
     * 用户注册
     *
     * @param param 用户提交表单数据
     * @return {
     * code:200 注册成功 300 用户已存在 400 注册失败 500 其他异常
     * <p>
     * }
     */
    @SuppressWarnings("all")
    @PostMapping("/register")
    public JSONObject user_register(@RequestBody(required = false) HashMap<String, Object> param) {
        int code;
        JSONObject res = JSONUtil.createObj();
        try {
            HashMap<String, Object> _param = (HashMap<String, Object>) param.get("param");
            if (!_param.isEmpty()) {
                // 注册到数据库
                code = userService.user_sign(_param);// 生成用户id
            } else {
                code = 400;
            }
        } catch (Exception e) {
            code = 500;
            e.printStackTrace();
        }
        res.putOnce("code", code);
        return res;
    }

    // 用户登陆
    @PostMapping(value = "/login")
    public JSONObject user_login(@RequestBody(required = false) HashMap<String, String> params) {
        return userService.user_login(params);
    }

    // 用户信息
    @PostMapping(value = "/info")
    @TokenValidate(auth_check = true, value = {"params"}, fileds = {"token"})
    public JSONObject user_info(@RequestBody(required = false) HashMap<String, String> params) {
        return null;
    }

    // 用户搜索
    @PostMapping(value = "/search")
    public ArrayList<HashMap<String, Object>> user_search(@RequestBody(required = false) HashMap<String, String> params) {
        return userService.user_search(params);
    }

    // 用户退出登录
    @GetMapping(value = "/logout")
    public JSONObject user_logout(@RequestParam("token") String token) {
        return userService.user_logout(token);
    }

    // 用户修改头像
    @PostMapping(value = "/avg_modify")
    public boolean user_avg_modify(@RequestParam("file_list") MultipartFile[] file_list, @RequestParam(name = "token") String token) {
        return fileService.user_avg_modify(file_list, token);
    }

    // 用户修改信息
    @PostMapping(value = "/info_modify")
    public JSONObject user_info_modify(@RequestBody(required = false) HashMap<String, String> params) {
        return userService.info_modify(params);
    }

    // 修改密码
    @PostMapping(value = "/modify/password")
    public JSONObject modify_password(@RequestBody(required = false) HashMap<String, String> params) {
        return userService.modify_password(params);
    }

    // 超级管理员接口
    @PostMapping(value = "/admin")
    public JSONObject admin(@RequestBody(required = false) HashMap<String, String> params) {
        return verity_admin(params);
    }

    // 获取所有用户列表
    @PostMapping(value = "admin/allusr")
    public JSONObject allUsr(@RequestBody(required = false) HashMap<String, String> params) {
        JSONObject obj = JSONUtil.createObj();
        if (verity_admin(params).getBool("is_admin")) {
            ArrayList<UserInfo> list = userService.allUsr();
            obj.putOpt("code", 200). // 无操作权限
                    putOpt("msg", "操作成功").
                    putOpt("data", list);
        } else {
            obj.putOpt("code", 400). // 无操作权限
                    putOpt("msg", "无操作权限");
        }
        return obj;
    }

    // 验证是否是超级管理员
    public synchronized JSONObject verity_admin(HashMap<String, String> params) {
        JSONObject query = userService.user_verity(params);
        if (Objects.isNull(query) || query.isEmpty()) {
            return null;
        }
        UserInfo uid = query.get("user_info", UserInfo.class);
        query.clear();// 清空
        query.putOpt("is_admin", userService.is_admin(uid.getUser_id()));
        return query;
    }

    // 显示所有用户（超级管理员专用）
    @PostMapping(value = "all_list")
    public JSONObject get_all_users(@RequestBody(required = false) HashMap<String, String> params) {
        JSONObject res = JSONUtil.createObj();
        if (Objects.isNull(params) || params.isEmpty()) {
            res.putOpt("code", 400)
                    .putOpt("msg", "参数错误");
        } else {
            JSONObject verityAdmin = verity_admin(params);
            if (Objects.nonNull(verityAdmin) && verityAdmin.getBool("is_admin")) {// 是超级管理员
                ArrayList<UserInfo> list = userService.get_all_users(params);
                res.putOpt("code", 200)
                        .putOpt("msg", "超级用户")
                        .putOpt("data", list);
            } else {
                res.putOpt("code", 500)
                        .putOpt("msg", "没有权限");
            }
        }
        return res;
    }

    /**
     * 设置用户状态
     *
     * @param params{ token:str,
     *                code:0[禁止登录],1[解除禁止]
     *                }
     */
    @PostMapping("set_status")
    public JSONObject set_status(@RequestBody(required = false) HashMap<String, String> params) {
        return userServiceAdmin.set_status(params); // 设置用户状态
    }
}
